import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/hj_utils/text_util.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../../hj_model/mine/OfficialListItemModel.dart';
import '../../../routers/jump_router.dart';
import '../../../tools_base/loading/l_error_widget.dart';
import '../../../tools_base/loading/loading_center_widget.dart';
import '../../../tools_base/widget/net_image_widget.dart';
import 'mine_drive_logic.dart';

class MineDrivePage extends StatelessWidget {
  MineDrivePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('加群开车')),
      body: GetBuilder<MineDriveLogic>(
        init: MineDriveLogic(),
        builder: (controller) {
          if (controller.allList == null) return const LoadingCenterWidget();
          if (controller.allList?.isEmpty == true)
            return CErrorWidget(
              retryOnTap: () {
                controller.loadData();
              },
            );
          return Container(
            margin: EdgeInsets.symmetric(horizontal: 16),
            child: CustomScrollView(
              slivers: [
                if (controller.groupList.isNotEmpty)
                  SliverToBoxAdapter(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        18.sizeBoxH,
                        Text(
                          '官方社群',
                          style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600),
                        ),
                        6.sizeBoxH,
                        Text(
                          '一起看片一起分享心得',
                          style: TextStyle(
                            color: Color(0xff525252),
                            fontSize: 12,
                          ),
                        ),
                        12.sizeBoxH,
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          decoration: BoxDecoration(
                            color: Colors.white.withOpacity(.05),
                            borderRadius: BorderRadius.circular(9),
                          ),
                          child: buildItem(controller.groupList),
                        )
                      ],
                    ),
                  ),
                if (controller.officialList.isNotEmpty)
                  SliverToBoxAdapter(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        18.sizeBoxH,
                        Text(
                          '商务合作',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        6.sizeBoxH,
                        Text(
                          '代理合作/商务合作',
                          style: TextStyle(
                            color: Color(0xff525252),
                            fontSize: 12,
                          ),
                        ),
                        12.sizeBoxH,
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          decoration: BoxDecoration(
                            color: Colors.white.withOpacity(.05),
                            borderRadius: BorderRadius.circular(9),
                          ),
                          child: buildItem(controller.officialList),
                        ),
                      ],
                    ),
                  ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget buildItem(List<OfficialListItemModel> list) {
    return Wrap(
      children: List.generate(list.length, (index) {
        OfficialListItemModel item = list[index];
        return InkWell(
          onTap: () => launchUrlToWeb(item.officialUrl ?? ''),
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 5),
            height: 80,
            alignment: Alignment.center,
            child: Row(
              children: [
                NetworkImageLoader(
                  imageUrl: item.officialImg ?? '',
                  width: 40,
                  height: 40,
                ),
                12.sizeBoxW,
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '${item.officialName}',
                        style: TextStyle(color: Colors.white, fontSize: 14, height: 1.1),
                      ),
                      if (TextUtil.isNotEmpty(item.officialDesc)) ...[
                        6.sizeBoxH,
                        Text(
                          '${item.officialDesc}',
                          style: TextStyle(color: Color(0xff525252), fontSize: 12, height: 1.1),
                        )
                      ]
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  height: 26,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: AppColors.actionRed,
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Text(
                    '立即加入',
                    style: TextStyle(color: Colors.white, fontSize: 12, fontWeight: FontWeight.w500),
                  ),
                )
              ],
            ),
          ),
        );
      }),
    );
  }
}
